-- -------------------------------------------------------------------------
-- UI/core.lua
-- UI System Coordinator
-- -------------------------------------------------------------------------

local UI = {}

local state = require("ClickUl.UI.state")
local input_module = require("ClickUl.UI.input")
local renderer = require("ClickUl.UI.renderer")
local notification = require("ClickUl.UI.notification")
local layout = require("ClickUl.UI.layout")
local widgets = require("ClickUl.UI.widgets")
local theme = require("ClickUl.UI.theme")

-- Features config (loaded on demand)
local features_config = nil

UI.state = state
UI.input = input_module
UI.renderer = renderer
UI.notification = notification
UI.layout = layout
UI.widgets = widgets
UI.theme = theme

function UI.init(config_sys, natives, debugger)
    state.set_dependency("config_sys", config_sys)
    state.set_dependency("natives", natives)
    state.set_dependency("debugger", debugger)

    if natives then
        local nc = state.natives_cache
        nc.DISABLE_CONTROL_ACTION = natives.DISABLE_CONTROL_ACTION
        nc.IS_CONTROL_JUST_PRESSED = natives.IS_CONTROL_JUST_PRESSED
        nc.IS_DISABLED_CONTROL_JUST_PRESSED = natives.IS_DISABLED_CONTROL_JUST_PRESSED
        nc.DISABLE_PLAYER_FIRING = natives.DISABLE_PLAYER_FIRING
        nc.PLAYER_ID = natives.PLAYER_ID
    end

    if not theme.data then theme.reset() end

    -- Initialize features config
    local ok, fc = pcall(require, "ClickUl.Config.features")
    if ok and fc then
        features_config = fc
        features_config.init(nil, debugger)
        UI.features_config = features_config
    end

    if debugger then debugger.info("[UI Core] Initialized") end
end

function UI.toggle_menu()
    state.ui.open = not state.ui.open
    if state.ui.open then
        renderer.force_update_scale()
        layout.refresh_background()
        state.ui.active_id = nil
        state.ui.active_combo = nil
        state.ui.text_input_active = false
        state.ui.scroll_consumed = false
        state.ui.tooltip_text = nil
    else
        -- Save features config when menu closes
        if features_config then
            features_config.auto_save()
        end
        UI.clear_cache()
    end
end

function UI.is_open() return state.ui.open end
function UI.draw_window(title, content_callback) layout.draw_window(title, content_callback) end
function UI.draw_tooltip() layout.draw_tooltip() end
function UI.draw_notifications() notification.draw() end
function UI.notify(msg, type_str) notification.notify(msg, type_str) end

function UI.get_active_tab() return state.ui.active_tab end
function UI.set_active_tab(idx) state.ui.active_tab = idx end
function UI.get_block_input() return state.ui.block_input end
function UI.set_block_input(val) state.ui.block_input = val end
function UI.set_settings_tab_index(idx) state.ui.settings_tab_index = idx end

function UI.clear_cache()
    -- Clear State
    if state.reset_cache then state.reset_cache() end

    -- Clear Renderer
    if renderer.clear_caches then renderer.clear_caches() end

    -- Clear Notification
    if notification.clear_cache then notification.clear_cache() end

    -- Clear Layout
    if layout.clear_failed_images then layout.clear_failed_images() end

    -- Clear Widgets
    if widgets.clear_cache then widgets.clear_cache() end

    -- Force GC
    if collectgarbage then collectgarbage("collect") end
end

function UI.update_colors()
    if state.reset_color_cache then state.reset_color_cache() end
    if renderer.reset_color_cache then renderer.reset_color_cache() end
end

function UI.force_update_scale() renderer.force_update_scale() end
function UI.refresh_background() layout.refresh_background() end
function UI.clear_failed_images() layout.clear_failed_images() end

-- Features Config API
function UI.save_features_config()
    if features_config then
        return features_config.save()
    end
    return false
end

function UI.get_features_config()
    return features_config
end

-- Convenience Exports
UI.Sidebar = layout.Sidebar
UI.SubTabBar = layout.SubTabBar
UI.SubTabBar2 = layout.SubTabBar2
UI.Group = layout.Group
UI.Collapsible = layout.Collapsible
UI.ScrollGroup = layout.ScrollGroup
UI.DualColumn = layout.DualColumn
UI.DualColumn_FixedLeft = layout.DualColumn_FixedLeft
UI.GetWidth = layout.GetWidth
UI.OverrideWidth = layout.OverrideWidth
UI.Row = layout.Row
UI.ButtonRow = layout.ButtonRow

UI.Button = widgets.Button
UI.Checkbox = widgets.Checkbox
UI.Toggle = widgets.Toggle
UI.Slider = widgets.Slider
UI.Combo = widgets.Combo
UI.TextInput = widgets.TextInput
UI.ColorPicker = widgets.ColorPicker
UI.Selectable = widgets.Selectable
UI.InlineTabBar = widgets.InlineTabBar
UI.Separator = widgets.Separator
UI.Text = widgets.Text
UI.ProgressBar = widgets.ProgressBar

UI.draw_rect = renderer.draw_rect
UI.draw_text = renderer.draw_text
UI.draw_rect_ex = renderer.draw_rect_ex
UI.draw_text_ex = renderer.draw_text_ex
UI.get_color = renderer.get_color
UI.get_color_alpha = renderer.get_color_alpha
UI.get_text_width = renderer.get_text_width
UI.truncate_text = renderer.truncate_text
UI.s = renderer.s
UI.vn = renderer.vn

return UI